package kr.co.bettersoft.waitnumbercustomer.fragment;


import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import kr.co.bettersoft.waitnumbercustomer.activity.EventListActivity;
import kr.co.bettersoft.waitnumbercustomer.activity.MemberStoreInfoActivity;
import kr.co.bettersoft.waitnumbercustomer.activity.PrefActivity;
import kr.co.bettersoft.waitnumbercustomer.activity.PrefCertiInfo;
import kr.co.bettersoft.waitnumbercustomer.activity.PrefPushOnOffActivity;
import kr.co.bettersoft.waitnumbercustomer.activity.PrefUserInfoActivity;
import kr.co.bettersoft.waitnumbercustomer.activity.R;
import kr.co.bettersoft.waitnumbercustomer.activity.myWebView;
import kr.co.bettersoft.waitnumbercustomer.common.CommonConstant;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 설정 -> 더보기 로 바꿈. 
 *  설정, 다른 앱 연결 등등..
 */
public class SettingsFragment extends Fragment {

	// 웹뷰용 인텐트
	Intent webIntent;
	
	// 폰트
	private Typeface typefaceNanumGothic= null; 
	private Typeface typefaceNanumGothicBold = null;
	
	TextView titleBar;		// 타이틀
	// 메뉴 목록
	Button seeMore1CertiInfoBtn;
	Button seeMore2SettingAlarm;
	Button seeMore3EventList;
	Button seeMore4Settings;
	Button seeMore5DayveLink;
	Button seeMore6CarrotLink;
	
	
	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 핸들러
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			try{
				Bundle b = msg.getData();		
				if(b.getInt("showToast")==1){		// 화면에 토스트 띄우기
					Toast.makeText(getActivity(), b.getString("msg"), Toast.LENGTH_SHORT).show();
				}
				if(b.getInt("showAlert")==1){					 // 경고창 . 
					//
					new AlertDialog.Builder(getActivity())
					.setTitle(CommonConstant.alertTitle)							
					.setMessage(b.getString("msg"))
					.setIcon(R.drawable.ic_dialog_img)		// 경고창 어플 아이콘으로바꿈.
					.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							// 그냥 사용자 확인 용이기 때문에 추가 조작 없음.
						}})
						.setNegativeButton("", null).show();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_settings, null);

		titleBar = (TextView) view.findViewById(R.id.title_bar);
		seeMore1CertiInfoBtn = (Button) view.findViewById(R.id.see_more_1_certi_info);
		seeMore2SettingAlarm = (Button) view.findViewById(R.id.see_more_2_setting_alarm);
		seeMore3EventList = (Button) view.findViewById(R.id.see_more_3_event_list);
		seeMore4Settings = (Button) view.findViewById(R.id.see_more_4_settings);
		seeMore5DayveLink = (Button) view.findViewById(R.id.see_more_5_dayve_link);
		seeMore6CarrotLink = (Button) view.findViewById(R.id.see_more_6_carrot_link);
		
		 // 폰트 지정.
	    typefaceNanumGothic = Typeface.createFromAsset(getActivity().getAssets(), CommonConstant.NanumGothic);  
	    typefaceNanumGothicBold = Typeface.createFromAsset(getActivity().getAssets(), CommonConstant.NanumGothicBold);
	    // 폰트 적용
	    titleBar.setTypeface(typefaceNanumGothic);	
		
		// Button listener
		seeMore1CertiInfoBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent PrefUserInfoIntent = new Intent(getActivity(), PrefCertiInfo.class);
				startActivity(PrefUserInfoIntent);
			}
		});
		seeMore2SettingAlarm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 공지 사항
//				Intent webIntent = new Intent(getActivity(), myWebView.class);
//				webIntent.putExtra("loadingURL", CommonConstant.noticeURL);
//				startActivity(webIntent);
				
				// 알림 설정
				Intent PrefPushOnOffIntent = new Intent(getActivity(), PrefPushOnOffActivity.class);
				startActivity(PrefPushOnOffIntent);
			}
		});
		seeMore3EventList.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent PushListIntent = new Intent(getActivity(), EventListActivity.class);
				startActivity(PushListIntent);
			}
		});
		seeMore4Settings.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), kr.co.bettersoft.waitnumbercustomer.activity.PrefActivity.class);
				startActivity(intent);
			}
		});
		seeMore5DayveLink.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				webIntent = new Intent(getActivity(), myWebView.class);
				webIntent.putExtra("loadingURL", CommonConstant.adDayveURL);
				startActivity(webIntent);
			}
		});
		seeMore6CarrotLink.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				webIntent = new Intent(getActivity(), myWebView.class);
				webIntent.putExtra("loadingURL", CommonConstant.adCarrotURL);
				startActivity(webIntent);
			}
		});
		
//		webIntent = new Intent(getActivity(), myWebView.class);
//		webIntent.putExtra("loadingURL", CommonConstant.adWaitNumberURL);
//		startActivity(webIntent);
		
//		webIntent = new Intent(getActivity(), myWebView.class);
//		webIntent.putExtra("loadingURL", CommonConstant.merchantRegisterURL);
//		startActivity(webIntent);
		
		return view;
	}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/*
	 * 핸들러를 통한 작업.
	 */
	// 화면에 토스트 띄움 msg 변수에 내용을 넣어서 호출하면 화면에 토스트를 띄운다.		// --> 통신 에러시에만.
	/**
	 * showMsg
	 *   화면에 토스트 띄움 msg 변수에 내용을 넣어서 호출하면 화면에 토스트를 띄운다
	 *
	 * @param msg
	 * @param 
	 * @return 
	 */
	public void showMsg(final String msg){			
		new Thread(
				new Runnable(){
					public void run(){
						Message message = handler.obtainMessage();				
						Bundle b = new Bundle();
						b.putInt("showToast", 1);
						b.putString("msg", msg);
						message.setData(b);
						handler.sendMessage(message);
					}
				}
		).start();
	} 
	/**
	 * showResultDialog
	 *  얼럿을 띄운다.
	 *
	 * @param msg
	 * @param 
	 * @return 
	 */
	public void showResultDialog(final String msg){
		new Thread(
				new Runnable(){
					public void run(){
						Message message = handler.obtainMessage();				
						Bundle b = new Bundle();
						b.putInt("showAlert", 1);
						b.putString("msg", msg);			// 화면에 보여줄 메시지
						message.setData(b);
						handler.sendMessage(message);
					}
				}
		).start();
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public void onResume(){
		super.onResume();
		FragmentHome.app_end = 0;
	}
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
}